<!--  -->
<template>
  <div>
    <div class='goods'>
      <div class="menu-wrapper"
           ref="menuWrapper">
        <ul>
          <li v-for='(item, index) in goods'
              :key='index'
              @click='selectMenu(index,$event)'
              class='menu-item'
              :class="{'current':currentIndex===index}">
            <span class='text'>
              <span class='icon'
                    v-show="item.type>0"
                    :class='classMap[item.type]'>
              </span>{{item.name}}
            </span>
          </li>
        </ul>
      </div>
      <div class="foods-wrapper"
           ref="foodWrapper">
        <ul>
          <li v-for='(item,index) in goods'
              :key='index'
              class='food-list food-list-hook'>
            <h1>{{item.name}}</h1>
            <ul>
              <li v-for='(food,i) in item.foods'
                  :key='i'
                  class='food-item'
                  @click='selectedFood(food,$event)'>
                <img class='img'
                     :src='food.icon'>
                <div class='right'>
                  <div class='name'>{{food.name}}
                    <span class='price'>￥{{food.price}}</span>
                  </div>
                  <span class='desc'>月销{{food.sellCount}}份
                    好评率{{food.rating}}%</span>
                </div>
                <div class='cartWrapper'>
                  <cartcontrol :food='food'
                               @cartadd='_drop'></cartcontrol>
                </div>
              </li>
            </ul>
          </li>
        </ul>
      </div>
    </div>

    <shopcart :selectFoods='selectFoods'
              ref='shopcart'></shopcart>
    <food :food='selectFood'
          ref='food'></food>
  </div>

</template>

<script type='text/ecmascript-6'>
import shopcart from '../shopcart/shopcart'
import cartcontrol from '../cartcontrol/cartcontrol'
import food from '../food/food'
import BScroll from 'better-scroll'

const ERR_OK = 0
export default {
  data () {
    return {
      goods: [],
      listHeight: [],
      scrollY,
      selectFood: {}
    }
  },
  components: {
    shopcart,
    cartcontrol,
    food
  },
  props: {
    seller: {
      type: Object
    }
  },
  computed: {
    selectFoods () {
      let foods = []
      this.goods.forEach((good) => {
        good.foods.forEach((food) => {
          if (food.count) {
            foods.push(food)
          }
        })
      })
      return foods
    },
    currentIndex () {
      for (let i = 0; i < this.listHeight.length; i++) {
        let height1 = this.listHeight[i]
        let height2 = this.listHeight[i + 1]
        if (!height2 || (this.scrollY >= height1 && this.scrollY < height2)) {
          return i
        }
      }
      return 0
    }
  },
  created () {
    this.classMap = ['decrease', 'discount', 'guarantee', 'invoice', 'special']
    this.$http.get('/api/goods').then((response) => {
      response = response.data
      if (response.errno === ERR_OK) {
        this.goods = response.data
        this.$nextTick(() => {
          this._initScroll()
          this._calculateHeight()
        })
      }
    })
  },
  methods: {
    _drop (target) {
      // 体验优化 异步执行drop动画
      this.$nextTick(() => {
        this.$refs.shopcart.drop(target)
      })
    },
    selectMenu (index, event) {
      if (!event._constructed) {
        // 非原生事件为true
        return
      }
      let foodList = this.$refs.foodWrapper.getElementsByClassName('food-list-hook')
      let el = foodList[index]
      this.foodsScroll.scrollToElement(el, 300)
    },
    _initScroll () {
      this.menuScroll = new BScroll(this.$refs.menuWrapper, {
        click: true
      })
      this.foodsScroll = new BScroll(this.$refs.foodWrapper, {
        probeType: 3, // 滚动的时候实时告诉我们位置
        click: true
      })
      this.foodsScroll.on('scroll', (pos) => {
        this.scrollY = Math.abs(Math.round(pos.y))
      })
    },
    _calculateHeight () {
      let foodList = this.$refs.foodWrapper.getElementsByClassName('food-list-hook')
      let height = 0
      this.listHeight.push(height)
      for (let i = 0; i < foodList.length; i++) {
        let item = foodList[i]
        height += item.clientHeight
        this.listHeight.push(height)
      }
    },
    selectedFood (food, event) {
      if (!event._constructed) {
        return
      }
      this.selectFood = food
      this.$refs.food.showFlag()
    }
  }
}

</script>
<style lang='stylus' scoped>
@import '../../common/stylus/mixin.styl'
@import '../../common/stylus/icon.styl'
@import '../../common/stylus/base.styl'
.shopcart
  position absolute
  left 0
  right 0
  bottom 0
.goods
  position absolute
  display flex
  overflow hidden
  width 100%
  top 174px
  bottom 58px
  .menu-wrapper
    flex 0 0 80px
    width 80px
    background #f3f5f7
    .menu-item
      height 54px
      width 56px
      line-height 14px
      display table
      padding 0 12px
      &.current
        background rgba(147, 153, 159, 0.2)
      .icon
        display inline-block
        vertical-align top
        width 12px
        height 12px
        background-size 12px 12px
        background-repeat no-repeat
        &.decrease
          bg-image('decrease_3')
        &.discount
          bg-image('discount_3')
        &.guarantee
          bg-image('guarantee_3')
        &.invoice
          bg-image('invoice_3')
        &.special
          bg-image('special_3')
      .text
        font-size 12px
        display table-cell
        vertical-align middle
        width 56px
  .foods-wrapper
    flex 1
    margin-left 18px
    .food-item
      position relative
      margin-top 18px
      .img
        width 60px
      .cartWrapper
        position absolute
        right 5px
        bottom 0
      .right
        position absolute
        top 0
        left 70px
        .name
          font-size 14px
          color rgb(7, 17, 27)
          line-height 14px
          .price
            font-size 14px
            color rgb(240, 20, 20)
        .desc
          color rgb(147, 153, 159)
          font-size 14px
          display block
          margin-top 8px
</style>
